﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Testcraft
{
    public struct PackedVertexPositionNormalTexture
    {
        public Vector3 Position;
        public int Normal;
        public int TexCoord;

        public PackedVertexPositionNormalTexture(Vector3 position, Vector3 normal, Vector2 texCoord)
        {
            this.Position = position;
            this.Normal = (int)(normal.X * 255.0f) | ((int)(normal.Y * 255.0f) << 8) | ((int)(normal.Z * 255.0f) << 16);
            //Vector3 n = new Vector3((this.normal & 0x000000FF) / 255.0f, ((this.normal & 0x0000FF00) >> 8) / 255.0f, ((this.normal & 0x00FF0000) >> 16) / 255.0f);
            //this.TexCoord = (int)(texCoord.X * short.MaxValue) | ((int)(texCoord.Y * short.MaxValue) << 16);
            this.TexCoord = (int)(texCoord.X) | ((int)(texCoord.Y) << 16);
            //Vector2 v = new Vector2((float)(this.TexCoord & 0x0000FFFF) / short.MaxValue,
            //    (float)((this.TexCoord & 0xFFFF0000) >> 16) / short.MaxValue);
        }

        public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration(
            new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
            new VertexElement(sizeof(float) * 3, VertexElementFormat.Byte4, VertexElementUsage.Normal, 0),
            new VertexElement((sizeof(float) * 3) + sizeof(int), VertexElementFormat.Short2, VertexElementUsage.TextureCoordinate, 0)
            );
    }
}
